package org.projetjava.presentation;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.projetjava.metier.model.HyperText;
import org.projetjava.metier.model.Lieu;
import org.projetjava.metier.model.News;
import org.projetjava.metier.model.Parcours;
import org.projetjava.service.GestionLieux;
import org.projetjava.service.GestionParcours;

public class DescriptionNewsDialogue extends JDialog{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private News news;
	private JLabel nomLabel;
	private JPanel panNom;
	private JLabel nomLabel2;
	private JPanel panNom2;
	private FenetrePrincipal fenetrePrincipal;

	private JPanel panel;

	private ImageSlider imageSlider;

	int positionImage = 0;
	
	private List<Image> listImage = new ArrayList<>();
	
	public int getPositionImage() {
		return positionImage;
	}

	public void setPositionImage(int positionImage) {
		this.positionImage = positionImage;
	}

	private JButton imgPrecedente = new JButton("<--"), 
			imgSuivante = new JButton("-->");
	
	public DescriptionNewsDialogue(News news ,JFrame parent, String title, boolean modal, FenetrePrincipal fenetrePrincipal)
	{
		
		super(parent, title, modal);
		this.setFenetrePrincipal(fenetrePrincipal);
		this.setSize(300, 550);
		this.setLocationRelativeTo(null);
		this.setResizable(true);
		
		Box contener = Box.createVerticalBox();
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		
		this.setNews(news);
		
		panNom = new JPanel();
		panNom.setBackground(Color.white);
		panNom.setMinimumSize(new Dimension(250, 60));
		panNom.setPreferredSize(new Dimension(250, 60));
		panNom.setMaximumSize(new Dimension(250, 60));
		panNom.setBorder(BorderFactory.createTitledBorder(news.getLibelle()));
		nomLabel = new JLabel(news.getDescription());
		panNom.add(nomLabel);
		
		panNom2 = new JPanel();
		panNom2.setBackground(Color.white);
		panNom2.setMinimumSize(new Dimension(250, 60));
		panNom2.setPreferredSize(new Dimension(250, 60));
		panNom2.setMaximumSize(new Dimension(250, 60));
		panNom2.setBorder(BorderFactory.createTitledBorder("Type : "+news.getTyperef()));
		nomLabel2 = new JLabel(news.getRef());
		nomLabel2.setCursor(new Cursor(Cursor.HAND_CURSOR));
		panNom2.add(nomLabel2);
		
		nomLabel2.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e) {
				
				
				if(getNews().getTyperef().equalsIgnoreCase("Lieu"))
				{
					GestionLieux gl = new GestionLieux();
					Lieu lieu = gl.selectionner(getNews().getRef());
					
					getFenetrePrincipal().initAffichageLieu(lieu);
					
					getFenetrePrincipal().runAffichageLieu();
				}else				
				{
					GestionParcours gp = new GestionParcours();
					Parcours parcours =  gp.selectionner(getNews().getRef());
				
					
					
					getFenetrePrincipal().initAffichageParcours(parcours);
					
					getFenetrePrincipal().runAffichageParcours();
				}
				setVisible(false);
				
		    }
		});

		
		panel = new JPanel();
		imageSlider = new ImageSlider();
		
		
		panel = new JPanel();
		imageSlider = new ImageSlider();
		JPanel sliderContenneur = new JPanel();
		sliderContenneur.add(imageSlider);
		panel.setBorder(BorderFactory.createTitledBorder("Liens :"));
		
		panel.setMinimumSize(new Dimension(250, 100));
		panel.setPreferredSize(new Dimension(250, 300));
		panel.setMaximumSize(new Dimension(250, 300));

		

		
		JPanel boutonImage = new JPanel();
		
		boutonImage.add(imgPrecedente);
		boutonImage.add(imgSuivante);
		
		imgPrecedente.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e) {
		        if(getPositionImage() > 0)
		        {
		        	setPositionImage(getPositionImage()-1);
		        	imageSlider.attribuerImage(listImage.get(getPositionImage()));
		        	repaint();
		        }

		    }
		});
		
		imgSuivante.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e) {
				if(getPositionImage() < (listImage.size()-1))
		        {
		        	setPositionImage(getPositionImage()+1);
		        	imageSlider.attribuerImage(listImage.get(getPositionImage()));
		        	repaint();
		        }
				
		    }
		});
		
		/*contener.add(panNom);
		contener.add(panel);
		contener.add(imageSlider);
		contener.add(boutonImage);*/

		contener.add(Box.createRigidArea(new Dimension(10,10)));
		contener.add(panNom);
		contener.add(Box.createRigidArea(new Dimension(10,10)));
		contener.add(panNom2);
		contener.add(Box.createVerticalGlue());
		contener.add(Box.createRigidArea(new Dimension(10,10)));
		contener.add(panel);
		contener.add(Box.createVerticalGlue());
		contener.add(Box.createRigidArea(new Dimension(10,10)));
		contener.add(imageSlider);
		contener.add(Box.createRigidArea(new Dimension(10,10)));
		contener.add(boutonImage);
		contener.add(Box.createVerticalGlue());
		contener.add(Box.createRigidArea(new Dimension(10,10)));
		
	
		this.add(contener);
		
		for(HyperText ht : news.getHypertext())
		{
			for(int i = 0 ; i < ht.getNomLienHyperText().size(); i++)
	        {
				JLabel lb_url = new JLabel();
		        lb_url.setText(ht.getNomLienHyperText().get(i));
		        lb_url.setCursor(new Cursor(Cursor.HAND_CURSOR));
		        addListener(lb_url, ht.getLienHyperText().get(i));
		        
		        panel.add(lb_url);
	        }
		}
		
		for(String img : news.getImages())
		{
			try {
			      Image image = ImageIO.read(new File(img));
			      this.listImage.add(image);
			     
			    } catch (IOException e) {
			      e.printStackTrace();
			    } 
		}
		
		
		if(listImage.size() > 0)
		{
			imageSlider.attribuerImage(listImage.get(0));
			this.repaint();
		}
		
		this.setVisible(true);
	}
	
	private void addListener(JLabel lb_url, String lien) {
		
        lb_url.addMouseListener(new ExecutionLienHyperText(lien));
    }

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public FenetrePrincipal getFenetrePrincipal() {
		return fenetrePrincipal;
	}

	public void setFenetrePrincipal(FenetrePrincipal fenetrePrincipal) {
		this.fenetrePrincipal = fenetrePrincipal;
	} 

}
